package com.deyou.adapter;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.deyou.R;
import com.deyou.activity.HouseSearchActivity;
import com.deyou.activity.HouseSearchResultListActivity;
import com.deyou.model.HomeDataBean;
import com.deyou.model.HomePageCityBean;
import com.deyou.util.AndroidUtils;
import com.deyou.util.GlideImageLoader;
import com.deyou.util.Urls;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Alex on 2017/3/24.
 */

public class MyGridViewAdapter extends BaseAdapter {

    private Context context;
    private List<HomeDataBean.HotCity> lists;//数据源
    private int mIndex; // 页数下标，标示第几页，从0开始
    private int mPagerSize;// 每页显示的最大的数量

    public void setLists(List<HomeDataBean.HotCity> lists) {
        this.lists = lists;
        notifyDataSetChanged();
    }

    public void setmIndex(int mIndex) {
        this.mIndex = mIndex;
    }

    public void setmPagerSize(int mPagerSize) {
        this.mPagerSize = mPagerSize;
    }

    public MyGridViewAdapter(Context context) {
        this.context = context;
    }

    /**
     * 先判断数据及的大小是否显示满本页lists.size() > (mIndex + 1)*mPagerSize
     * 如果满足，则此页就显示最大数量lists的个数
     * 如果不够显示每页的最大数量，那么剩下几个就显示几个
     */
    @Override
    public int getCount() {
        return lists.size() > (mIndex + 1) * mPagerSize ?
                mPagerSize : (lists.size() - mIndex * mPagerSize);
    }

    @Override
    public HomeDataBean.HotCity getItem(int arg0) {
        return lists.get(arg0 + mIndex * mPagerSize);
    }

    @Override
    public long getItemId(int arg0) {
        return arg0 + mIndex * mPagerSize;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.city_gridview_item_layout, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        //重新确定position因为拿到的总是数据源，数据源是分页加载到每页的GridView上的
        final int pos = position + mIndex * mPagerSize;//假设mPageSiez
        //假设mPagerSize=8，假如点击的是第二页（即mIndex=1）上的第二个位置item(position=1),那么这个item的实际位置就是pos=9
        holder.tvCity.setText(lists.get(pos).regionName + "");
        new GlideImageLoader().displayImage(context, TextUtils.isEmpty(lists.get(position).picurl) ? R.drawable.img_default : Urls.ImageBaseUrl + "homecitypic/" + lists.get(position).picurl, holder.ivCity);
        holder.rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putString("lonAndLat", "");
                bundle.putString("regionCode", lists.get(pos).regionId);
                bundle.putString("myCurrentCity", "");
                AndroidUtils.startNextActivity((Activity) context, HouseSearchResultListActivity.class, bundle, false);
            }
        });
        return convertView;
    }

    class ViewHolder {
        @BindView(R.id.iv_city)
        ImageView ivCity;
        @BindView(R.id.tv_city)
        TextView tvCity;
        @BindView(R.id.root_view)
        LinearLayout rootView;

        ViewHolder(View view) {
            AutoUtils.auto(view);
            ButterKnife.bind(this, view);
        }
    }
}
